<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="付款日期:">
        <el-date-picker v-model="queryParams.paymentDate" type="date" value-format="yyyy-MM-dd" size="small" placeholder="选择付款日期" clearable />
      </el-form-item>
      <el-form-item label="收款主體編號,系统内有维护上下级关系:">
        <el-input v-model="queryParams.paymentOrgNo" size="small" placeholder="请输入收款主體編號,系统内有维护上下级关系" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="收款主:">
        <el-input v-model="queryParams.paymentOrgName" size="small" placeholder="请输入收款主" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="分帳金額:">
        <el-input v-model="queryParams.paymentAmount" size="small" placeholder="请输入分帳金額" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="业务单据号:">
        <el-input v-model="queryParams.basicNo" size="small" placeholder="请输入业务单据号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="业务单据总金额:">
        <el-input v-model="queryParams.orderTotAmount" size="small" placeholder="请输入业务单据总金额" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单来源:">
        <el-input v-model="queryParams.orderSource" size="small" placeholder="请输入订单来源" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="订单数量:">
        <el-input v-model="queryParams.wtCnt" size="small" placeholder="请输入订单数量" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['module:commsionDetail:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['module:commsionDetail:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['module:commsionDetail:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
    </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="付款日期" align="center" prop="paymentDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.paymentDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="收款主體編號,系统内有维护上下级关系" align="center" prop="paymentOrgNo" />
      <el-table-column label="收款主" align="center" prop="paymentOrgName" />
      <el-table-column label="分帳金額" align="center" prop="paymentAmount" />
      <el-table-column label="业务单据号" align="center" prop="basicNo" />
      <el-table-column label="业务单据总金额" align="center" prop="orderTotAmount" />
      <el-table-column label="付款方式" align="center" prop="paymentType" />
      <el-table-column label="订单来源" align="center" prop="orderSource" />
      <el-table-column label="订单数量" align="center" prop="wtCnt" />
      <el-table-column label="订单状态" align="center" prop="orderStatus" />
      <el-table-column label="佣金结算状态" align="center" prop="status" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button v-hasPermission="['module:commsionDetail:update']" size="mini" type="text" icon="el-icon-edit" @click="handleEdit(scope.row)">修改</el-button>
          <el-button v-hasPermission="['module:commsionDetail:delete']" size="mini" type="text" icon="el-icon-delete" @click="handleRemove(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!-- 添加/修改对话框 -->
    <el-dialog :title="commsionDetailFormDialog.title" :visible.sync="commsionDetailFormDialog.visible" :close-on-click-modal="false" :before-close="resetCommsionDetailFormDialog" width="500px" append-to-body>
      <el-form ref="commsionDetailForm" :model="commsionDetailFormData" :rules="commsionDetailFormRules" label-width="80px">
        <el-form-item label="付款日期" prop="paymentDate">
          <el-date-picker v-model="commsionDetailFormData.paymentDate" type="date" value-format="yyyy-MM-dd" size="small" style="width: 200px" placeholder="选择付款日期" clearable />
        </el-form-item>
        <el-form-item label="收款主體編號,系统内有维护上下级关系" prop="paymentOrgNo">
          <el-input v-model="commsionDetailFormData.paymentOrgNo" placeholder="请输入收款主體編號,系统内有维护上下级关系" />
        </el-form-item>
        <el-form-item label="收款主" prop="paymentOrgName">
          <el-input v-model="commsionDetailFormData.paymentOrgName" placeholder="请输入收款主" />
        </el-form-item>
        <el-form-item label="分帳金額" prop="paymentAmount">
          <el-input v-model="commsionDetailFormData.paymentAmount" placeholder="请输入分帳金額" />
        </el-form-item>
        <el-form-item label="业务单据号" prop="basicNo">
          <el-input v-model="commsionDetailFormData.basicNo" placeholder="请输入业务单据号" />
        </el-form-item>
        <el-form-item label="业务单据总金额" prop="orderTotAmount">
          <el-input v-model="commsionDetailFormData.orderTotAmount" placeholder="请输入业务单据总金额" />
        </el-form-item>
        <el-form-item label="订单来源" prop="orderSource">
          <el-input v-model="commsionDetailFormData.orderSource" placeholder="请输入订单来源" />
        </el-form-item>
        <el-form-item label="订单数量" prop="wtCnt">
          <el-input v-model="commsionDetailFormData.wtCnt" placeholder="请输入订单数量" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="commsionDetailFormData.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmCommsionDetailFormDialog">确 定</el-button>
        <el-button @click="resetCommsionDetailFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import CommsionDetailApi from '@/api/accounting/CommsionDetailApi'

const defaultCommsionDetailFormData = {
  paymentDate: null,
  paymentOrgNo: null,
  paymentOrgName: null,
  paymentAmount: null,
  basicNo: null,
  orderTotAmount: null,
  paymentType: null,
  orderSource: null,
  wtCnt: null,
  orderStatus: '0',
  status: '0'
}
export default {
  name: 'CommsionDetailList',
  data() {
    return {
      options: {
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        paymentDate: null,
        paymentOrgNo: null,
        paymentOrgName: null,
        paymentAmount: null,
        basicNo: null,
        orderTotAmount: null,
        paymentType: null,
        orderSource: null,
        wtCnt: null,
        orderStatus: null,
        status: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      commsionDetailFormDialog: {
        title: '',
        visible: false
      },
      commsionDetailFormData: Object.assign({}, defaultCommsionDetailFormData),
      commsionDetailFormRules: {
        paymentDate: [{ required: true, message: '付款日期不能为空', trigger: 'blur' }],
        paymentOrgNo: [{ required: true, message: '收款主體編號,系统内有维护上下级关系不能为空', trigger: 'blur' }]
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
    },
    handleAdd() {
      this.openCommsionDetailFormDialog()
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return CommsionDetailApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      this.openCommsionDetailFormDialog(id)
    },
    handleQuery() {
      this.loading = true
      CommsionDetailApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return CommsionDetailApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    openCommsionDetailFormDialog(id) {
      this.commsionDetailFormDialog.visible = true
      if (id) {
        this.commsionDetailFormDialog.title = '财务佣金结算明细,交易发生后产生佣金支付的明细，可管理和追踪付款状态-修改'
        CommsionDetailApi.get(id).then(response => {
          this.commsionDetailFormData = {
            ...this.commsionDetailFormData,
            ...response.data
          }
        })
      } else {
        this.commsionDetailFormDialog.title = '财务佣金结算明细,交易发生后产生佣金支付的明细，可管理和追踪付款状态-添加'
      }
    },
    confirmCommsionDetailFormDialog() {
      this.$refs['commsionDetailForm'].validate(valid => {
        if (valid) {
          if (this.commsionDetailFormData.id) {
            CommsionDetailApi.edit(this.commsionDetailFormData).then(response => {
              this.$message.success('修改成功')
              this.resetCommsionDetailFormDialog()
              this.handleQuery()
            })
          } else {
            CommsionDetailApi.add(this.commsionDetailFormData).then(response => {
              this.$message.success('添加成功')
              this.resetCommsionDetailFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetCommsionDetailFormDialog() {
      this.commsionDetailFormDialog = {
        title: '',
        visible: false
      }
      this.commsionDetailFormData = Object.assign({}, defaultCommsionDetailFormData)
      this.resetForm('commsionDetailForm')
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
      this.resetForm('queryForm')
      this.handleQuery()
    }
  }
}
</script>
